/*Hdip Project 2013-14, Student Name:- Chris Pembroke,  Student Number:- x00115839*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.ComponentModel.DataAnnotations; // needed additional namespace ref
using Exercise1Project.Models.Validation;
using System.Web.Mvc;
using System.Text.RegularExpressions;

namespace Exercise1Project.Models  
{
    public class BusinessLogicService : IBusinessLogicService  // a business logic layer placed between the controller and database loosely coupled via interface
    {
        private IValidationDictionary _validationDictionary;  // implements validation logic via interface to modelstate wrapper class
        private IBikeRepository _repository;                  // implements database logic via repository interface

        public BusinessLogicService(IValidationDictionary validationDictionary) : this(validationDictionary, new BikeRepository()) // constructor used in test mock repository
        {

        }

        public BusinessLogicService(IValidationDictionary validationDictionary, IBikeRepository repository) // constructor used in implementing non test logic
        {
            _validationDictionary = validationDictionary;
            _repository = repository;
        }

        // validation business logic
        public bool ValidateBike (Bike bikeToValidate)  
        {
            if (bikeToValidate.Make == null || bikeToValidate.Make.Trim().Length == 0)                      // null or trim().length checks if null or empty
                _validationDictionary.AddError("Make", "Bike Make is Required Field!");

            if (bikeToValidate.Model == null || bikeToValidate.Model.Trim().Length == 0)
                _validationDictionary.AddError("Model", "Bike Model is Required Field!");

            if (bikeToValidate.Color == null || bikeToValidate.Color.Trim().Length == 0)
                _validationDictionary.AddError("Color", "Bike Color is Required Field!");

            return _validationDictionary.IsValid;
        }

        // database business logic
        public bool AddCustomerBike(Bike bikeToAdd)
        {
            //validation
            if (!ValidateBike(bikeToAdd))
                return false;

            //database
            try
            {
                
                _repository.AddCustomerBike(bikeToAdd);
            }
            catch
            {
                 return false;
            }
            return true;
        }

        public bool UpdateBikeDetail(Bike bikeToUpdate)
        {
            //validation
            if(!ValidateBike(bikeToUpdate))
                return false;

            //database
            try
            {
                _repository.UpdateBikeDetail(bikeToUpdate);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool RemoveCustomerBike(int id)
        {
           //validation none for delete
            //database
            try
            {
                _repository.RemoveCustomerBike(id);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public Bike GetBikeByBikeId(int id)
        {
           return  _repository.GetBikeByBikeId(id);
        }

        public IEnumerable<Bike> GetBikeByCustomerID(string id)
        {
            return _repository.GetBikeByCustomerID(id);
        }

    }
}